package com.ganzalang.gmall.designpatterns.command;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: 使用命令模式
 * @author: ganzalang
 */
@Slf4j
public class CommandPatternDemo {

    /**
     * 命令模式就是将请求封装成一个对象，将命令的请求者与执行者解耦，invoker是请求者
     * @param args
     */
    public static void main(String[] args) {
        CommandInvoker commandInvoker = new CommandInvoker();
        commandInvoker.setCommand(new CommandA());
        commandInvoker.execute();
    }

    public interface Command {
        void execute();
    }

    public static class CommandA implements Command {

        @Override
        public void execute() {
            log.info("执行命令A");
        }
    }

    public static class CommandB implements Command {

        @Override
        public void execute() {
            log.info("执行命令B");
        }
    }

    public static class CommandInvoker {
        private Command command;

        private Command getCommand() {
            return  command;
        }

        private void setCommand(Command command) {
            this.command = command;
        }

        public void execute() {
            command.execute();
        }
    }


}
